package com.yonyou.cloud.member.adapters.openapi.controller;

import com.yonyou.cloud.member.adapters.dto.ApiResponseDto;
import com.yonyou.cloud.member.domainmodel.entities.coupon.Coupon;
import com.yonyou.cloud.member.domainmodel.vo.CouponId;
import com.yonyou.cloud.member.domainmodel.vo.MemberId;
import com.yonyou.cloud.member.domainmodel.services.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "卡券服务接口")
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    @GetMapping("/effective/{member}")
    @ApiOperation(value = "获取会员可用卡券", httpMethod = "GET")
    @ApiResponse(code = 200, message = "卡券列表", response = Coupon.class, responseContainer = "List")
    public ApiResponseDto<List<Coupon>> effectiveCoupons(@ApiParam(name = "member", value = "会员ID", required = true) @PathVariable("member") Long id) {
        List<Coupon> coupons = couponService.effectiveCoupons(new MemberId(id));
        return new ApiResponseDto(true).setData(coupons);
    }

    @GetMapping("/consume/{id}")
    @ApiOperation(value = "消费优惠券", httpMethod = "GET")
    @ApiResponse(code = 200, message = "是否成功", response = Boolean.class)
    public ApiResponseDto<Boolean> consume(@ApiParam(name = "id", value = "优惠券ID", required = true) @PathVariable("id") Long id) {
        Boolean result = couponService.consume(new CouponId(id));
        return new ApiResponseDto(result);
    }

    @GetMapping("/reset")
    @ApiOperation(value = "重置所有优惠券使其可用，仅用于测试", httpMethod = "GET")
    @ApiResponse(code = 200, message = "是否成功", response = Boolean.class)
    public ApiResponseDto<Boolean> reset() {
        Boolean result = couponService.reset();
        return new ApiResponseDto(result);
    }
}